const request = require("request");
const { ipcRenderer } = require("electron");
const { cmdOpenTool } = require("../util/xcx_cmd");
const idePort = require("./ide_port");
/**
 *  请求
 * @param {Object} option
 */
ipcRenderer.on("message-box-response", (event, responese) => {
	console.log(responese);
	if (responese === 1) {
		try {
			cmdOpenTool();
			idePort.refreshIdePort();
			console.log(idePort.getIdePort());
		} catch (error) {
			console.log(error);
		}
	}
});
exports.req = (option = {}) => {
	return new Promise((resolve, reject) => {
		const callback = (error, response, body) => {
			if (error) {
				if (error.message.includes("connect ECONNREFUSED")) {
					//小程序未启动
					ipcRenderer.send("open-message-box", {
						message: "检测到微信开发者工具未启动,是否启动?",
					});
				}
				reject(error);
			}
			resolve([response, body]);
		};
		request(option, callback);
	});
};
